#include <allegro.h>
#include "minigame.hpp"
#include <iostream>
#include "game.hpp"
#include <cstdlib>

extern cGame  *game;

 cMiniGame::cMiniGame(int weight,int height)
{
    card0= load_bmp("Data\\BitMe\\0.bmp",default_palette);
    card1= load_bmp("Data\\BitMe\\1.bmp",default_palette);
    card2= load_bmp("Data\\BitMe\\2.bmp",default_palette);
    card3= load_bmp("Data\\BitMe\\3.bmp",default_palette);
    card4= load_bmp("Data\\BitMe\\4.bmp",default_palette);
    card5= load_bmp("Data\\BitMe\\5.bmp",default_palette);
    card6= load_bmp("Data\\BitMe\\6.bmp",default_palette);
    card7= load_bmp("Data\\BitMe\\7.bmp",default_palette);
    card8= load_bmp("Data\\BitMe\\8.bmp",default_palette);
    card9= load_bmp("Data\\BitMe\\9.bmp",default_palette);
    effect1= load_bmp("Data\\BitMe\\Effects\\boom.bmp",default_palette);

    minigamemenu = load_bmp("Data\\BitMe\\menu.bmp",default_palette);

    isexit=0;
    cornerminigame = new sPoint ((weight-minigamemenu->w)/2,(height-minigamemenu->h)/2);
    playerturn=1;
    playerpoints=0;
    computerpoints=0;
    drawnumberbitmap=card0;

    lastclikedpos[0]=0;
    lastclikedpos[1]=1;

    roundnumber=0;
}



void cMiniGame::drawgame(BITMAP * bufor,int bid)
{
    game->pause=1;
    preparegame();

    while (!key[KEY_P] || isexit==1 )
    {
        masked_blit(minigamemenu,bufor, 0,0,cornerminigame->x,cornerminigame->y,minigamemenu->w,minigamemenu->h);//rysowanie menu
        masked_blit(drawnumberbitmap,bufor,0,0,167+cornerminigame->x,60+cornerminigame->y ,drawnumberbitmap->w,drawnumberbitmap->h);//rysowanie aktywnie wylosowanej karty


        if (mouse_b==1 && mouse_x != lastclikedpos[0] && mouse_y != lastclikedpos[1])
            {

                if (mouse_x > cornerminigame->x+440 && mouse_y > cornerminigame->y + 515) {isexit=1 ; game->pause=0;  return ;} //przycisk wyjscia
                if (mouse_x > cornerminigame->x+140 &&  mouse_x< cornerminigame->x+200 && mouse_y > cornerminigame->y + 480) bid *= 2; //podwojenie stawki

                            if (mouse_x > cornerminigame->x + 15 && mouse_x < cornerminigame->x + 77 && mouse_y > cornerminigame->y + 480 && playerturn == 1) //przycisk + - prosba o kolejna karte
                            {
                                draw(playerpoints);
                                if (playerpoints>11) {game->hero->eq->itemPull(0,bid); game->log->addMessage("Przegrales !");isexit=1;game->pause=0;return;}
                                else
                                if (playerpoints==11) {game->hero->eq->itemAdd(0,bid); game->log->addMessage("Wygrales !");isexit=1;game->pause=0;return;}
                            }

                if (mouse_x > cornerminigame->x + 77 && mouse_x < cornerminigame->x + 141 && mouse_y > cornerminigame->y + 480 && playerturn ==1) //stop
                {
                    playerturn=0;

                            do
                            {

                                draw(computerpoints);
                                blit(bufor, screen, 0, 0, 0, 0, bufor->w, bufor->h);
                            masked_blit(drawnumberbitmap,bufor,0,0,167+cornerminigame->x,60+cornerminigame->y ,drawnumberbitmap->w,drawnumberbitmap->h);//rysowanie aktywnie wylosowanej karty
                            textout_ex( bufor, font, itoa(playerpoints,(char*)tmp.c_str(),10),cornerminigame->x + 45,cornerminigame->y + 460, makecol( 0, 0, 0 ), - 1 );  //wyrysowanie punktow gracza
                            textout_ex( bufor, font, itoa(computerpoints,(char*)tmp.c_str(),10),cornerminigame->x + 315,cornerminigame->y + 460, makecol( 0, 0, 0 ), - 1 );  //wyrysowanie punktow komputera

                            rest(500);
                            animation(effect1, minigamemenu, 230 , 310 , 5 ,50 , 80 ,0);


                            }while (computerpoints<7);

                        if (playerpoints==11 || playerpoints>computerpoints) {game->hero->eq->itemAdd(0,bid); game->log->addMessage("Wygrales !"); isexit=1;game->pause=0;return;}
                        if (computerpoints==11 || computerpoints>playerpoints){game->hero->eq->itemPull(0,bid); game->log->addMessage("Przegrales !");  isexit=1;game->pause=0;return;}

                        if (playerpoints==computerpoints)   {game->log->addMessage("Remis !");  isexit=1;game->pause=0;return;}




                }

                lastclikedpos[0]=mouse_x;
                lastclikedpos[1]=mouse_y;

        }


    textout_ex( bufor, font, itoa(playerpoints,(char*)tmp.c_str(),10),cornerminigame->x + 45,cornerminigame->y + 460, makecol( 0, 0, 0 ), - 1 );  //wyrysowanie punktow gracza
    textout_ex( bufor, font, itoa(computerpoints,(char*)tmp.c_str(),10),cornerminigame->x + 315,cornerminigame->y + 460, makecol( 0, 0, 0 ), - 1 );  //wyrysowanie punktow komputera
    blit(bufor, screen, 0, 0, 0, 0, bufor->w, bufor->h);
    }
game->pause=0;
return;
}


void cMiniGame::draw(int &who)
{
    drawnumber = ( rand() % 10 ) + 1;
    who += drawnumber;

    switch (drawnumber)
    {
        case 1:
            drawnumberbitmap = card0;
            break;

        case 2:
            drawnumberbitmap = card1;
            break;

        case 3:
            drawnumberbitmap = card2;
            break;

        case 4:
            drawnumberbitmap = card3;
            break;

        case 5:
            drawnumberbitmap = card4;
            break;

        case 6:
            drawnumberbitmap = card5;
            break;

        case 7:
            drawnumberbitmap = card6;
            break;

        case 8:
            drawnumberbitmap = card7;
            break;

        case 9:
            drawnumberbitmap = card8;
            break;

        case 10:
            drawnumberbitmap = card9;
            break;
    }


}



void cMiniGame::preparegame()
{

playerpoints=0;
computerpoints=0;
roundnumber=0;
playerturn=1;

}

void cMiniGame::animation(BITMAP * bitmap,BITMAP * bufor,  int X , int Y , int count_cages ,int weight_cages , int time ,bool copybufor)
{
    for (int i=0 ; i < count_cages ; i++)
    {
        masked_blit(bitmap, bufor,i*weight_cages,0,X,Y,weight_cages,weight_cages);

        if (copybufor==1)
        blit(bufor,screen, 0, 0, 0, 0, bufor->w, bufor->h);

        rest(time);
    }



}











